package com.cskaoyan;

import com.cskaoyan.bean.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * 创建日期: 2022/03/28 10:12
 *
 * @author ciggar
 */
public class Main {

    public static void main(String[] args) throws IOException {

        // SQlSessionFactory 构建器
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        // 读取配置文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        // 通过类加载器来读取

        /**
         * 创建日期：2022/03/28 11:09
         * @param args
         * @return void
         * @author ciggar
         *
         *         ClassLoader classLoader = Main.class.getClassLoader();
         *
         *         URL url = classLoader.getResource("mybatis-config.xml");
         *
         *         String path = url.getPath();
         *
         *         FileInputStream fileInputStream = new FileInputStream(path);
         *
         *         //可以简化为：
         *         ClassLoader classLoader = Main.class.getClassLoader();
         *         InputStream fileInputStream = classLoader.getResourceAsStream("mybatis-config.xml");
         */

        // 获取SQLSessionFactory
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);

        // 获取SQLSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 通过SQLSession执行SQL语句
        Account account = sqlSession.selectOne("cskaoyan.selectAccountById", 2);


        System.out.println(account);

        // 关闭连接
        sqlSession.close();



    }
}
